package com.idopartx.phonelightning.ui.permissionguide

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.dotools.umlibrary.UMPostUtils
import com.idopartx.phonelightning.MainActivity
import com.idopartx.phonelightning.R
import com.idopartx.phonelightning.common.Constant
import com.idopartx.phonelightning.databinding.ActivityGeneralSettingBinding
import com.idopartx.phonelightning.utils.PermissionUtils
import com.idopartx.phonelightning.utils.PreferenceSetting
import com.tencent.mmkv.MMKV

/****小米 OPPO 7,8,9***/
class Help3PermissionActivity : AppCompatActivity() {
    var isJumpAutoStart = false
    var isJumpBackgroundRun = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        DataBindingUtil.setContentView<ActivityGeneralSettingBinding>(
            this,
            R.layout.activity_general_setting
        )

        /**自启动按钮**/
        findViewById<Button>(R.id.start_autostart_btn).setOnClickListener {
            UMPostUtils.onEvent(applicationContext, "authority_auto_click")

            OppoAutoStartGuideDialog(this, object : OppoAutoStartGuideDialog.DialogCallBack {
                override fun onOk(index: Int) {
                    UMPostUtils.onEvent(
                        applicationContext,
                        "authority_auto_jump"
                    )
                    PermissionUtils.startAutoStartSetting(this@Help3PermissionActivity)
                    isJumpAutoStart = true
                    updatePermissionStatus()
                }
            }).show()
        }

        /**后台运行**/
        findViewById<Button>(R.id.start_background_btn).setOnClickListener {

            UMPostUtils.onEvent(applicationContext, "authority_allow_click");
            when {
                PermissionUtils.isOppoDevice() -> {
                    if (Build.VERSION.SDK_INT >= 27) {     //8.0及以上
                        UMPostUtils.onEvent(
                            applicationContext,
                            "guide_oppo8_9auto_go_on_click"
                        )
                        OppoBackgroundRunGuideDialog(this,
                            object : OppoBackgroundRunGuideDialog.DialogCallBack {
                                override fun onOk(index: Int) {
                                    UMPostUtils.onEvent(
                                        applicationContext,
                                        "authority_allow_jump"
                                    )
                                    PermissionUtils.startOppoPowerManager(this@Help3PermissionActivity)
                                    isJumpBackgroundRun = true
                                    updatePermissionStatus()
                                }
                            }).show()
                    } else {      //7.0及以下
                        PermissionUtils.startWhiteList(this)
                        isJumpBackgroundRun = true
                        updatePermissionStatus()
                    }
                }
                PermissionUtils.isXiaomiDevice() -> {
                    RemoveLimitDialog(this, object : RemoveLimitDialog.DialogCallBack {
                        override fun onOk() {
                            PermissionUtils.startXiaoMiBackRun(this@Help3PermissionActivity);
                            isJumpBackgroundRun = true
                            updatePermissionStatus()
                        }
                    }).show()

                }
            }
        }

        findViewById<Button>(R.id.next_step).setOnClickListener {
//            MMKV.defaultMMKV().encode(Constant.MMKV_IS_GUIDE, true)
            PreferenceSetting.setFirst(this, false)
            UMPostUtils.onEvent(this, "authority_finish_click")
            val intent = Intent(this, MainActivity::class.java)
            intent.putExtra("isSplash", true)
            startActivity(intent)
            finish()
        }

    }

    fun updatePermissionStatus() {
        if (isJumpAutoStart) {
            findViewById<Button>(R.id.start_autostart_btn).setBackgroundResource(R.drawable.shape_solid_42d174_radius_4)
            findViewById<Button>(R.id.start_autostart_btn).text = "已开启"
        } else {
            findViewById<Button>(R.id.start_autostart_btn).setBackgroundResource(R.drawable.shape_solid_3ac8bf_radius_4)
        }

        if (isJumpBackgroundRun) {
            findViewById<Button>(R.id.start_background_btn).setBackgroundResource(R.drawable.shape_solid_42d174_radius_4)
            findViewById<Button>(R.id.start_background_btn).text = "已开启"
        } else {
            findViewById<Button>(R.id.start_background_btn).setBackgroundResource(R.drawable.shape_solid_3ac8bf_radius_4)
        }
    }

    override fun onResume() {
        super.onResume()
        updatePermissionStatus()
    }
}